package com.fyyg.common.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;



/**
 * 请求响应帮助类,可以响应普通文本,html,xml和json对象
 * @author zdx
 * 
 */
public final class ResponseUtil {
	

	
	/**
	 * 发送文本。使用UTF-8编码。
	 * @param response HttpServletResponse
	 * @param text 发送的字符串
	 * @throws IOException 
	 */
	public static void renderText(HttpServletResponse response, String text) throws IOException {
		render(response, "text/plain;charset=UTF-8", text);
	}
	
	
	
	/**
	 * 发送html。使用UTF-8编码
	 * @param response
	 * @param html
	 * @throws IOException
	 */
	public static void rendHtml(HttpServletResponse response, String html) throws IOException{
		render(response, "text/html;charset=UTF-8", html);
	}
	
	
	/**
	 * 发送json形式的html
	 * @param response
	 * @param key
	 * @param value
	 * @throws IOException
	 */
	public static void rendJsonHtml(HttpServletResponse response,String key, String value) throws IOException{
		JSONObject json=new JSONObject();
		json.put(key, value);
		render(response, "text/html;charset=UTF-8", json.toString());
	}
	
	
	/**
	 * 发送json 使用UTF-8编码。
	 * @param response HttpServletResponse
	 * @param text 发送的字符串
	 * @throws IOException 
	 */
	public static void renderJson(HttpServletResponse response, String text) throws IOException {
		render(response, "application/json;charset=UTF-8", text);
	}
	
	/**
	 * 发送json 使用UTF-8编码。
	 * @param response HttpServletResponse
	 * @param text 发送的字符串
	 * @throws IOException 
	 */
	public static void renderJsonN(HttpServletResponse response, String text){
		try {
			render(response, "application/json;charset=UTF-8", text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 发送包含单个键值对的json,使用UTF-8编码
	 * @param response
	 * @param name
	 * @param value
	 * @throws IOException
	 */
	public static void renderJson(HttpServletResponse response,String name,String value) throws IOException{
		JSONObject json=new JSONObject();
		json.put(name, value);
		render(response, "application/json;charset=UTF-8", json.toString());
	}
	
	
	
	/**
	 * 发送xml 使用UTF-8编码。
	 * @param response HttpServletResponse
	 * @param text 发送的字符串
	 * @throws IOException 
	 */
	public static void renderXml(HttpServletResponse response, String text) throws IOException {
		render(response, "text/xml;charset=UTF-8", text);
	}
	

	
	/**
	 * 发送内容  （可设置文字格式）
	 * @param response
	 * @param contentType 文本格式
	 * @param text 内容
	 * @throws IOException 
	 */
	private static void render(HttpServletResponse response, String contentType,
			String text) throws IOException {
		response.setContentType(contentType);
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		PrintWriter out=response.getWriter();
		out.write(text);
		out.close();
	}
	
}
